package com.unknowpupil.web.tool;

import lombok.extern.slf4j.Slf4j;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class MD5Tool {

    public static String md5(String plainText){
        byte [] secretBytes = null;
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            secretBytes = md.digest();
        }catch (NoSuchAlgorithmException e){
            throw new RuntimeException("没有MD5这个算法");
        }
        String md5Code = new BigInteger(1,secretBytes).toString(16);
        for(int i=0;i<32-md5Code.length();i++){
            md5Code="0"+md5Code;
        }
        return md5Code;
    }

}
